package com.cxs.shopstore.handler;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxs.shopstore.models.Good;
import com.cxs.shopstore.models.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.util.Base64;

/*
 * @Author:cxs
 * @Date:2021/5/7  16:31
 * @Version 1.0
 * 图片处理器，用于将路径对应文件转换为base64格式字符串
 * */
@Slf4j
@Component
public class ImgHandler {
    //分页器图片转换
    public void ByteToBase64(IPage<Good> page){
        for (Good record : page.getRecords()) {
            try {
                byte[] bytes = FileCopyUtils.copyToByteArray(new File(record.getGoodImgPath()));
                record.setGoodImgPath(Base64.getEncoder().encodeToString(bytes));
            } catch (IOException e) {
                log.error("",e);
                e.printStackTrace();
            }
        }
    }
 }
